package com.wm.practice;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CompletableFutureApiDemo
 * @Description: CompletableFuture的Api示例
 * @Author: Deamer
 * @Date: 2022/9/3 16:28
 **/
public class CompletableFutureApiDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);// 需要等待2秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "abc";
        });
        try {
            TimeUnit.SECONDS.sleep(1);// 需要等待1秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // //执2-等s1 返回默认值110
        System.out.println(completableFuture.getNow("默认值110"));
        //执2-等1s 返回true+备胎值completeValue
        System.out.println(completableFuture.complete("completeValue") + "\t" + completableFuture.get());
    }
}
